package mk.company.fin.thread;

import javax.swing.JLabel;

import mk.company.fin.frame.FinFrame;
import mk.company.fin.main.GuiApplication;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StartApplication implements Runnable {

	private FinFrame frame;
	private JLabel loadingLabel;
	private boolean openMenu;
	
	public StartApplication(FinFrame frame) {
		this(frame, null);
	}
	
	public StartApplication(FinFrame frame, JLabel loadingLabel) {
		this.frame = frame;
		this.loadingLabel = loadingLabel;
	}
	
	public StartApplication(FinFrame frame, boolean openMenu ) {
		this.frame = frame;
		this.openMenu=openMenu;
	}
	@Override
	public void run() {
		if (frame.getContext() == null) {
			ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/applicationContext.xml");
			frame.initialize(context);
			
			// remove the label and set the location
			frame.getContentPane().remove(loadingLabel);
			frame.setLocationRelativeTo(null);
			
			// repaint
			frame.paintAll(frame.getGraphics());
			setSizeDependingComponentProperties();
			
		} else {
			// only init
			if (openMenu)
				frame.initComponents2();
			else
				frame.initComponents();
		}
		
		// repaint and set size properties
		frame.paintAll(frame.getGraphics());
		setSizeDependingComponentProperties();
		
	}
	
	private void setSizeDependingComponentProperties() {
		frame.getSplitPane().setDividerLocation(GuiApplication.SPLIT_DEVIDER_LOCATION);
	}

}
